package the.formula.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Momentum extends Activity{
        
    private double answer = 0;
    private LinearLayout ll;
    private TextView tv;
    private EditText mass;
    private EditText velocity;
    private EditText secondMass;
    private EditText secondVelocity;
    private boolean answerAdded;
    
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.momentum_activity);
        ll = (LinearLayout)findViewById(R.id.momentumLayout);
        tv = new TextView(this);
        mass = (EditText) findViewById(R.id.firstMass);
        velocity = (EditText)findViewById(R.id.firstVelocity);
        secondMass = (EditText) findViewById(R.id.secondMass);
        secondVelocity = (EditText)findViewById(R.id.secondVelocity);
    }
        
    public void onClick(View v) {
    	if(answerAdded) {
        	ll.removeView(tv);
        }
        switch(v.getId()) {
            case R.id.solveMomentum:
            	solveMomentum("momentum");
                break;
            case R.id.solveMass:
                solveMomentum("mass");
            case R.id.solveVelocity:
                solveMomentum("velocity");
                break;
        }
        ll.addView(tv);
        answerAdded = true;
    }

    private void solveMomentum(String str) {
    	String sMass = mass.getText().toString();
    	String sVelocity = velocity.getText().toString();
    	double massNum = Double.parseDouble(sMass);
    	double velocityNum = Double.parseDouble(sVelocity);
    	if(str.equals("momentum")) {
    		momentum(massNum, velocityNum);
    		tv.setText("Momentum: " + answer);
    	} else if(str.equals("mass")) {
    		String sSecondVelocity = secondVelocity.getText().toString();
    		double secondVelocityNum = Double.parseDouble(sSecondVelocity);
    		secondMass(massNum, velocityNum, secondVelocityNum);
    		tv.setText("Mass: " + answer);
    	} else if(str.equals("velocity")) {
    		String sSecondMass = secondMass.getText().toString();
    		double secondMassNum = Double.parseDouble(sSecondMass);
    		secondVelocity(massNum, velocityNum, secondMassNum);
    		tv.setText("Velocity: " + answer);
    	}
	}

	private void momentum(double mass, double velocity) {
        double result = mass * velocity;
        answer = result;
    }
        
    private void secondMass(double firstMass, double firstVelocity, double secondVelocity) {
        double result = firstMass * firstVelocity / secondVelocity;
        answer = result;
    }
    
    private void secondVelocity(double firstMass, double firstVelocity, double secondMass) {
        double result = firstMass * firstVelocity / secondMass;
        answer = result;
    }
}